package com.cindata

import com.cindata.dic.PrecinctTypeDict

class CustomRegion
{

    String name
    // 区域名称
    PrecinctTypeDict precinctType
    // 区域类型
    String description
    // 描述
    District district
    // 所属行政区
    Date createDate
    // 创建时间
    Date modifyDate
    // 修改时间
    Double x
    //x
    Double y
    //y
    String coordinates
    //边界坐标
    Boolean isDeleted
    // 是否删除
    Integer sourceId
    // 源对象ID
    String sourceClass
    // 源对象ClassName
    Double zoom
    // 地图放大级别
    String idList
    //押品id

    static mapping = {
        autoImport false
        precinctType enumType: 'string'
        version false
    }

    static constraints = {
        name maxSize: 50, nullable: true, blank: true, comment: '名称'
        precinctType maxSize: 30, nullable: true, blank: true, comment: '区域类型'
        description maxSize: 100, nullable: true, blank: true, comment: '描述'
        district nullable: true, blank: true, comment: '所属行政区'
        createDate nullable: true, blank: true, comment: '创建时间'
        modifyDate nullable: true, blank: true, comment: '修改时间'
        x nullable: true, blank: true, comment: 'x'
        y nullable: true, blank: true, comment: 'y'
        coordinates maxSize: 1500, nullable: true, blank: true, comment: '边界坐标'
        isDeleted nullable: true, blank: true, comment: '是否删除'
        sourceId nullable: true, blank: true, comment: '源对象ID'
        sourceClass maxSize: 100, nullable: true, blank: true, comment: '源对象ClassName'
        zoom nullable: true, blank: true, comment: '地图放大级别'
        idList maxSize: 1500, nullable: true, blank: true, comment: '押品id'
    }

    def toHM()
    {
        def hashObj = [id: this.id,
            name: this?.name,
            precinctType: this?.precinctType,
            parentId: null,
            description: this?.description,
            district: this?.district,
            x: this?.x,
            y: this?.y,
            coordinates: this?.coordinates,
            isDeleted: this?.isDeleted,
            sourceId: this?.sourceId,
            sourceClass: this?.sourceClass,
            zoom: this?.zoom,
            idList: this?.idList,
            type: "CustomRegion",
            children: []]
        return hashObj
    }

    static ToHashMapArr(customRegionArr)
    {
        def hashArr = []
        customRegionArr.each {
            hashArr.push(it.toHM())
        }
        return hashArr
    }

    // 对机构树填充自定义区域数据
    static PushCustomRegionIntoSourceTree(sourceArr, sourceClass)
    {
        def customRegionArr = CustomRegion.findAllBySourceClass(sourceClass)
        customRegionArr = ToHashMapArr(customRegionArr)
        sourceArr.each { it -> PushCustomRegionIntoNode(it, customRegionArr)
        }
    }

    // 对单个机构节点进行自定义区域填充
    static PushCustomRegionIntoNode(source, customRegionArr)
    {
        def tmpArr = customRegionArr.findAll { it.sourceId == source.id }
        source.children.each { it -> PushCustomRegionIntoNode(it, customRegionArr)
        }
        if (tmpArr.size() > 0)
        {
            source.children.addAll(tmpArr)
        }
    }
}
